package cn.com.nxt.txb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import cn.com.nxt.txb.fragment.MineFragment;
import cn.com.nxt.txb.httpdelegate.MeetingDelegate;
import cn.com.nxt.txb.httpdelegate.UserDelegate;
import cn.com.nxt.txb.model.User;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.AuthPreferences;
import cn.com.nxt.txb.widget.ClearEditText;

@ContentView(R.layout.activity_modify_username)
public class ModifyUsernameActivity extends BaseActivity {
    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.et_old_password)
    private ClearEditText mOldPwdEt;
    @ViewInject(R.id.et_new_password)
    private ClearEditText mNewPwdEt;
    private byte userNameFlag = 0x004;
    private byte meetingNameFlag = 0x005;
    private UserDelegate userDelegate;
    private MeetingDelegate meetingDelegate;
    private String mOldPassword, mNewPassword;
    @ViewInject(R.id.tv_title)
    private TextView mTitleTv;
    @ViewInject(R.id.tv_save)
    private TextView mSaveTv;
    @ViewInject(R.id.view_line)
    private View mLineView;
    private boolean isMeeting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        isMeeting = getIntent().getBooleanExtra("meeting", false);
        setSupportActionBar(toolbar);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        //toolbar.setNavigationIcon(R.drawable.back_main);
        if (isMeeting) {
            mTitleTv.setText("名称");
            mNewPwdEt.setHint("请输入新的会议室名");
            mSaveTv.setVisibility(View.VISIBLE);
            mLineView.setVisibility(View.VISIBLE);
            meetingDelegate = new MeetingDelegate(this);
        } else {
            userDelegate = new UserDelegate(this);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!isMeeting)
            getMenuInflater().inflate(R.menu.modify_password, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.save_new:
                save();
                break;
        }
        return true;
    }

    @Event({R.id.tv_save})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.tv_save:
                save();
                break;
        }
    }

    private void save() {
        //       mOldPassword = mOldPwdEt.getText().toString().trim();
//        if (TextUtils.isEmpty(mOldPassword)) {
//            showToast("当前用户名不能为空！");
//            return;
//        }
        mNewPassword = mNewPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mNewPassword)) {
            if (isMeeting)
                showToast("会议室名不能为空！");
            else
                showToast("新用户名不能为空！");
            return;
        }
        if (!isMeeting) {
            mOldPassword = AppUtil.mUser.getUsername();
            Map<String, String> map = new HashMap<>();
            map.put("phone", AuthPreferences.getUserAccount());
            map.put("oldname", mOldPassword);
            map.put("newname", mNewPassword);
            Gson gson = new Gson();
            showDialog("正在修改");
            Log.v("--name--", gson.toJson(map));
            userDelegate.modifyUsername(gson.toJson(map), AppUtil.mUser.getToken(), userNameFlag);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("name", mNewPassword);
            Gson gson = new Gson();
            showDialog("正在修改");
            Log.v("--name--", gson.toJson(map));
            meetingDelegate.setMeetingInfo(AppUtil.mUser.getToken(), gson.toJson(map), meetingNameFlag);
        }
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        disDialog();
        if (!isMeeting) {
            AppUtil.mUser.setUsername(mNewPassword);
            AppUtil.saveUser(ModifyUsernameActivity.this, AppUtil.mUser);
        }else{
            MyMeetingActivity.meetingName =mNewPassword;
//            JSONObject obj = null;
//            try {
//                obj = new JSONObject(result);
//                MyMeetingActivity.meetingName = obj.optString("name");
//                MyMeetingActivity.meetingName = obj.optString("roomid");
//            } catch (JSONException e) {
//            }
        }
        showToast("修改成功");
        setResult(UserInfoActivity.RESULT_NAME, new Intent());
        finish();
    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {
        super.failure(result, flag, status);
        disDialog();
        showToast(result);
    }

    @Override
    public void finished(int flag) {
        disDialog();
    }

    @Override
    public void cancelled(int flag) {
        disDialog();
    }

}
